package com.bonc.modules.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.bonc.common.utils.*;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.modules.business.entity.TbCommonParamInfoEntity;
import com.bonc.modules.business.entity.TbWorkOrderInfoEntity;
import com.bonc.modules.business.entity.TbWorkOrderReproceeEntity;
import com.bonc.modules.business.service.TbWorkOrderInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bonc.modules.business.dao.TbWorkOrderArchiveDao;
import com.bonc.modules.business.entity.TbWorkOrderArchiveEntity;
import com.bonc.modules.business.service.TbWorkOrderArchiveService;

@DataSource("data")
@Service("tbWorkOrderArchiveService")
public class TbWorkOrderArchiveServiceImpl extends ServiceImpl<TbWorkOrderArchiveDao, TbWorkOrderArchiveEntity> implements TbWorkOrderArchiveService {

    @Autowired
    private  TbWorkOrderArchiveService tbWorkOrderArchiveService;

    @Autowired
    private TbWorkOrderInfoService tbWorkOrderInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TbWorkOrderArchiveEntity> page = this.page(
                new Query<TbWorkOrderArchiveEntity>().getPage(params),
                new QueryWrapper<TbWorkOrderArchiveEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(TbWorkOrderArchiveEntity tbWorkOrderArchive) {
        return SqlHelper.retBool(this.getBaseMapper().insert(tbWorkOrderArchive));
    }

    @Override
    public List<Map<String, Object>> queryArchiveList() {
        return this.baseMapper.queryArchiveList();
    }

    @Override
    public AtomicReference<String> postOrderArchiveService(TbWorkOrderArchiveEntity orderArchive) {
        /*LambdaQueryWrapper<TbWorkOrderArchiveEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<TbWorkOrderArchiveEntity> list = tbWorkOrderArchiveService.list(queryWrapper);*/

        AtomicReference<String> res = new AtomicReference<>("");
        AtomicReference<String> req = new AtomicReference<>("");

       /*list.forEach(orderArchive->{

        });*/

        TbWorkOrderInfoEntity tbWorkOrderInfo = tbWorkOrderInfoService.getById(orderArchive.getIdentifier());
        if(tbWorkOrderInfo.getState().equals("05")){//已归档
            System.out.println("该工单已归档");
        }else{
            String url = "http://10.254.30.252:3900/api/v2/sync/CSVC/StatementCSS";
            SupermanUtil util = new SupermanUtil();

            JSONObject content = new JSONObject();
            content.put("Identifier", orderArchive.getIdentifier());
            content.put("FilingOpinion", orderArchive.getFilingOpinion());

            Map<String, Object> param = ResolveParamUtils.getAllParam(content);

            req.set(JSON.toJSONString(param));
            res.set(util.sendPost(url, req.get()));

            tbWorkOrderInfo.setIdentifier(orderArchive.getIdentifier());
            tbWorkOrderInfo.setState("05");
            tbWorkOrderInfoService.updateById(tbWorkOrderInfo);

            tbWorkOrderArchiveService.save(orderArchive);
        }

        return res;
    }

    @Override
    public ReplyCommon StatementCSS(TbCommonParamInfoEntity tbCommonParamValue) {
        String content = "";
        String rspCode = "";
        String rspDesc = "";

        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat1= new SimpleDateFormat("yyyyMMddHHmmss");
        String cutOffDay = dateFormat.format(calendar.getTime());
        String handingTime = dateFormat1.format(calendar.getTime());
        String transIDO = handingTime + System.currentTimeMillis();
        String providePartyID = "CSVB3510";

        /* 网状网已经校验 暂时屏蔽
        String signjy = ResolveParamUtils.analysisAllParam(tbCommonParamValue);
        if(sign == signjy || sign.equals(signjy)){

        }else{
            rspCode = "20207";
            rspDesc = "非法的签名";
        }*/
        content = tbCommonParamValue.getContent();
        TbWorkOrderArchiveEntity tbWorkOrderArchive = JSONObject.parseObject(content,TbWorkOrderArchiveEntity.class);
        if(StringUtils.isBlank(tbWorkOrderArchive.getIdentifier()) || StringUtils.isBlank(tbWorkOrderArchive.getFilingOpinion())){
            rspCode = "20101";
            rspDesc = "缺少必须的参数";
        }else{
            //通过工单编号 判断该工单是否存在  不存在返回报错信息  存在进行下一步操作
            TbWorkOrderInfoEntity tbWorkOrderInfo = tbWorkOrderInfoService.getById(tbWorkOrderArchive.getIdentifier());
            if(tbWorkOrderInfo == null){
                rspCode = "20000";
                rspDesc = "此工单不存在，请检查！";
            }else{
                if(tbWorkOrderInfo.getState().equals("04")){ //只有处理完成 即已回复 的工单能够归档
                    //将工单状态更改为已归档
                    tbWorkOrderInfo.setState("05");
                    tbWorkOrderInfoService.updateById(tbWorkOrderInfo);
                    boolean num = tbWorkOrderArchiveService.save(tbWorkOrderArchive);
                    if(num == true){
                        rspCode = "00000";
                        rspDesc = "success";
                    }else{
                        rspCode = "20000";
                        rspDesc = "基础服务系统内部错误";
                    }
                }else if(tbWorkOrderInfo.getState().equals("05")){
                    rspCode = "20000";
                    rspDesc = "当前工单已归档";
                }else{
                    rspCode = "20000";
                    rspDesc = "当前工单未处理完成";
                }
            }
        }
        return ReplyCommon.ok(transIDO,cutOffDay,providePartyID,rspCode,rspDesc);
    }

    @Override
    public TbWorkOrderArchiveEntity getById(String id) {
        return this.getBaseMapper().selectById(id);
    }

}
